<template>
	<view>
		<view class="list">
			<view class="item" >
				<text class="key">当前证件照:</text>
				<view class="container">
					<image :src="avater" mode="widthFix" class="photo"></image>
				</view>
			</view>
		</view>
		<uni-section title="请选择证件照" type="line">
			<view class="example-body">
				<uni-file-picker
					file-mediatype="image"
					disable-preview
					:del-icon="false"
					mode="grid"
					ref="file"
					file-extname="png,jpg"
					:limit="1"
					:auto-upload="false"
					:source-type=source
					:maxMb="1"
					v-if='isSelect'
					@select="getPhotoPath"
				></uni-file-picker>
			</view>
		</uni-section>
		<view class="register-desc">
			请上传JPG或PNG格式的图片，图片大小不超过1MB，上传的图片不能带有蒙版或其他无法解析的内容，一般正常的证件照图片即可上传
		</view>
		<button class="save-btn" @tap="save">确定</button>
	</view>
</template>

<script>
	import uniFilePicker from '@/components/uni-file-picker/uni-file-picker.vue';
	import uniSection from '@/components/uni-section/uni-section.vue';
	export default {
		components: {
			uniFilePicker,
			uniSection
		},
		data() {
			return {
				avater:'',
				source:['album'],
				tempFilePaths:[],
				isSelect:true
			}
		},
		onLoad:function(){
			let that = this;
			that.ajax(that.url.getuserinfo,'POST',null,function(resp){
				let data = resp.data.userinfo.user;
				that.avater = data.avater;
				if(that.avater==null||that.avater.length<=0){
					that.isSelect=false;
					uni.showToast({
						title:'当前用户无证件照，请添加后再操作',
						icon:'none'
					});
					setTimeout(function(){
						uni.switchTab({
							url: "../mine/mine"
						})
					},1000);
				}
			})
		},
		methods: {
			getPhotoPath(e){
				let that = this;
				that.tempFilePaths = e.tempFilePaths;
			},
			save(){
				let that = this;
				if(that.tempFilePaths==null){
					uni.showToast({
						title:'无法获取照片',
						icon:'error'
					});
					setTimeout(function(){
						uni.switchTab({
							url: "../mine/mine"
						})
					},1000)
				}else{
					uni.showModal({
						title:'提示',
						content:'是否确认修改证件照',
						success:function(e){
							if(e.confirm){
								uni.uploadFile({
									url:that.url.updateAvater,
									filePath:that.tempFilePaths[0],
									name:"files",
									header:{
										token:uni.getStorageSync("token")
									},
									formData:{
										type:'avater'
									},
									success:(uploadFileRes) => {
										let data = JSON.parse(uploadFileRes.data);
										uni.showToast({
											icon: 'success',
											title: "修改成功",
										});
										setTimeout(function(){
											that.avater = data.result;
											uni.switchTab({
												url: "../mine/mine"
											})
										},2000)
										
									}
								});
							}
						}
					});
				}
			}
		}
	}
</script>

<style lang="less">
	@import url('./modified_avater.less');
</style>
